package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description NursingProjectServiceImpl
 * @Author songyu
 * @Date 2024-10-15
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    /**
     * 分页查询护理项目
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //1.定义PageHelper的页码和每页大小
        PageHelper.startPage(pageNum,pageSize);

        //2.执行分页查询数据返回Page<NursingProjectVo>
        Page<NursingProjectVo> page = nursingProjectMapper.findByCondition(name,status);

        //3.封装给PageResponse<NursingProjectVo>
        PageResponse<NursingProjectVo> pageResponse = new PageResponse<>(page);//里面会自动封装页码，每页大小，总条数，总页数
        pageResponse.setRecords(page.getResult());//封装当前页数据列表

        //4.返回数据
        return pageResponse;
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDTO
     */
    @Override
    public void add(NursingProjectDto nursingProjectDTO) {
        //1.将nursingProjectDTO转换为nursingProject
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDTO, NursingProject.class);

        //2.调用mapper插入数据库
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据id查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        return nursingProjectMapper.findById(id);
    }

    /**
     * 修改护理项目
     *
     * @param nursingProjectDTO
     */
    @Override
    public void update(NursingProjectDto nursingProjectDTO) {
        //将nursingProjectDTO 准话为 nursingProject
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDTO, NursingProject.class);

        //调用mapper修改
        nursingProjectMapper.update(nursingProject);
    }
    /**
     * 修改状态
     * @param id 项目id
     * @param status 状态 0 启用 , 1 禁用
     */
    @Override
    public void putStatus(Long id, Integer status) {
        NursingProject nursingProject = new NursingProject();
        nursingProject.setId(id);
        nursingProject.setStatus(status);
        nursingProjectMapper.update(nursingProject);
    }
    /**
     * 删除项目
     * @param id
     */
    @Override
    public void delete(Long id) {
        nursingProjectMapper.delete(id);
    }
    /**
     * 查询所有护理项目
     * @return 护理项目视图对象列表
     */
    @Override
    public List<NursingProjectVo> getAll() {
        return nursingProjectMapper.getAll();
    }
}
